<?php
declare (strict_types=1);

namespace app\school\model\school\admin;

use app\admin\model\user\Account;
use app\common\model\BaseModel;
use think\model\relation\BelongsTo;

/**
 * 定义机构历史数据模型
 */
class SchoolBalanceFlow extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'school_balance_flow';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;
    /**
     * 不写入创建时间
     *
     * @var boolean
     */
    protected $updateTime = false;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义字段类型
     *
     * @var array
     */
    protected $type = [
        'id'         => 'integer',
        'flow_type'  => 'integer',
        'price'      => 'float',
        'balance'    => 'float',
        'real_price' => 'float',
    ];

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 新增数据事件
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-03-03
     *
     * @return   string
     */
    public static function onBeforeInsert($log)
    {
        $log->admin_user_id = request()->admin_user_id;
    }

    /**
     * 关联的用户名
     *
     * @return BelongsTo
     */
    public function userName(): BelongsTo
    {
        return $this->belongsTo(Account::class, 'admin_user_id')->removeOption('soft_delete')->removeWhereField('mhm_id')->bind(['admin_user_name' => 'user_name']);
    }
}
